-
Notifications
You must be signed in to change notification settings - Fork 30
/
Write-PPErrorEventLog.ps1
66 lines (52 loc) · 1.79 KB
/
Write-PPErrorEventLog.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
$Metadata = @{
Title = "Write PowerShell PowerUp Error Event Log"
Filename = "Write-PPEventLog.ps1"
Description = ""
Tags = "powershell, profile, function, write, error, event, log"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-10-21"
LastEditDate = "2013-11-18"
Version = "1.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Write-PPErrorEventLog{
<#
.SYNOPSIS
If an error occured this function writes an event log.
.DESCRIPTION
If an error occured this function writes an event log.
.PARAMETER Message
Message for the event log entry. The Error log will be add after this message.
.PARAMETER Source
Source parameter for the event log entry.
.PARAMETER ClearErrorVariable
Clear PowerShell error variable.
.EXAMPLE
PS C:\> Write-PPErrorEventLog -Message "An error occured!" -Source "MyApplication" -ClearErrorVariable
#>
param(
[Parameter(Mandatory=$false)]
[string]$Message,
[Parameter(Mandatory=$false)]
[string]$Source,
[switch]
$ClearErrorVariable
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
if($Error){
$Error | %{$ErrorLog += "$($_.ToString()) $($_.InvocationInfo.PositionMessage) `n`n"}
if($Message){$ErrorLog = "$Message `n`n" + $ErrorLog}
if($ClearErrorVariable){$Error.clear()}
Write-PPEventLog -Message $ErrorLog -Source $Source -EntryType Error -WriteMessage
}
}